//
//  SettingViewController.m
//  AoJi
//
//  Created by Administrator on 15/1/20.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//

#import "SettingViewController.h"
#import "LoginViewController.h"


@interface SettingViewController ()
{
    
}

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    //背景图片
//    UIImageView * bg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bj"]];
//    bg.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
//    [self.view addSubview:bg];
    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bj"]];
    
    
    //返回按钮
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame=CGRectMake(0, 32, 44, 44);
    [back setImage:[UIImage imageNamed:@"fanhuibai"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    //设置图片在按钮中得偏移位置
    //back.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 55);
    [self.view addSubview:back];
    
    //title标题
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-50, 32, 100, 44)];
    label.text=@"设置";
    label.textColor=[UIColor whiteColor];
    label.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    //如果有用户显示退出按钮
    if ([kUserDefaults objectForKey:kAccessToken]) {
        //退出按钮
        quit = [UIButton buttonWithType:UIButtonTypeCustom];
        quit.frame=CGRectMake(self.view.frame.size.width-60, 32, 44, 44);
        [quit setTitle:@"退出" forState:UIControlStateNormal];
        quit.titleLabel.font=[UIFont systemFontOfSize:15];
        [quit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [quit addTarget:self action:@selector(quit) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:quit];
    }
    
    
    //logo图片
    UIImageView*logo = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-75, 99, 150, 150)];
    logo.image=[UIImage imageNamed:@"logo"];
    [self.view addSubview:logo];
    
    //根据屏幕等比例放大或缩小
//    UIImageView*logo = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2-75*([UIScreen mainScreen].bounds.size.width)/320, 99, 150*([UIScreen mainScreen].bounds.size.width)/320, (([UIScreen mainScreen].bounds.size.height == 480)?(150*(([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0 ?([UIScreen mainScreen].bounds.size.height-20):([UIScreen mainScreen].bounds.size.height)))/480):(150*(([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0 ?([UIScreen mainScreen].bounds.size.height-20):([UIScreen mainScreen].bounds.size.height)))/568)))];
//    logo.image=[UIImage imageNamed:@"logo"];
//    [self.view addSubview:logo];
    
    
    //tableview
    array = @[@"新消息通知",@"清除缓存",@"关于澳际",@"去AppStore评价"];
    tableView = [[UITableView alloc]init];
    //cell高度array.count*56.5
    tableView.frame=CGRectMake(0, 99+logo.frame.size.height+42, self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(logo.frame)-42);
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.backgroundColor=[UIColor clearColor];
    //分割线
    //tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    //隐藏多余的cell
    UIView *footerView = [[UIView alloc]init];
    footerView.backgroundColor = [UIColor clearColor];
    tableView.tableFooterView=footerView;
    

    
}

//返回
-(void)back
{
    [[NSNotificationCenter defaultCenter]postNotificationName:kSideMenuOpen object:@(4)];
    [_drawer open];
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark 退出登录
//退出登录
-(void)quit
{
    //判断是否有用户
//    if ([kUserDefaults objectForKey:kAccessToken]) {
//        UIAlertView *quitAlertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定退出登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"返回", nil];
//        quitAlertView.tag = 250;
//        [quitAlertView show];
//    }else {
//        
//        UIAlertView *quitAlertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"亲,您还没有登录" delegate:self cancelButtonTitle:@"赶快去登录" otherButtonTitles:@"我想再看看", nil];
//        quitAlertView.tag = 251;
//        [quitAlertView show];
//    }
    
    UIAlertView *quitAlertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定退出登录" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"返回", nil];
    quitAlertView.tag = 250;
    [quitAlertView show];
    
}
//跳转登录
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==250) {
        if (buttonIndex == 0) {
            //退出登录,清空用户数据
//            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"login"];
            [kUserDefaults removeObjectForKey:kAccessToken];//清除token
            [kUserDefaults synchronize];//立即生效
            [quit removeFromSuperview]; //从view删除
            quit = nil; //清空内存
            
            alert = [[UIAlertView alloc]initWithTitle:nil message:@"您已退出" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            //[self performSelector:@selector(alert) withObject:self afterDelay:3];
        }
    }
//    else {
//    if (buttonIndex == 0) {
//        LoginViewController *loginview = [[LoginViewController alloc]init];
//        [self.navigationController pushViewController:loginview animated:YES];
//        //[self presentViewController:loginview animated:YES completion:nil];
//      }
//    }
}


#pragma mark tableview
//分区返回多少行cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return array.count;
}

//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56.5;
}

//cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indetifier=@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetifier];
        
        if (indexPath.row ==0) {
            //通知
            messageNotification=[[UISwitch alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60, 10, 0, 0)];
            [cell.contentView addSubview:messageNotification];
        }
        if (indexPath.row ==1) {
            UILabel *memoryLab = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-60, 11, 50,35 )];
            memoryLab.text=@"%iMB";
            memoryLab.textColor=[UIColor whiteColor];
            [cell.contentView addSubview:memoryLab];
        }
    }
    cell.backgroundColor=[UIColor clearColor];
    cell.textLabel.text=array[indexPath.row];
    cell.textLabel.font=[UIFont systemFontOfSize:18];
    cell.textLabel.textColor=[UIColor whiteColor];
    
//    //自定义cell分割线
//    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, .5)];
//    line.image=[UIImage imageNamed:@"fengjiexian"];
//    [cell.contentView addSubview:line];
    

    
    if (indexPath.row == 2 || indexPath.row == 3) {
        //系统默认样式
        cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
        
//        UIImageView *more = [[UIImageView alloc]initWithFrame:CGRectMake(100, 0, 20, 20)];
//        more.image=[UIImage imageNamed:@"more"];
//        [cell.contentView addSubview:more];
    }
    
    return cell;
}

//cell   -------   select
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            NSLog(@"新消息通知");
        }
            break;
        case 1:
        {
            //拿到路径删除文件
            NSLog(@"清楚缓存");
            
            //删除NSUserDefaults记录
            NSString *_memory=[[NSBundle mainBundle]bundleIdentifier];
            [[NSUserDefaults standardUserDefaults]removePersistentDomainForName:_memory];
            NSLog(@"%@",_memory);
            
        }
            break;
        case 2:
        {
            NSLog(@"关于澳际");
            AboutAOJIViewController *aboutVC = [[AboutAOJIViewController alloc]init];
            [self.navigationController pushViewController:aboutVC animated:YES];
        }
            break;
            
        default:
        {
            //去AppStore评价  修改应用id
            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=587767923"]];
            NSLog(@"去AppStore评价");
        }
            break;
    }
}


#pragma mark 消息开关


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - ICSDrawerController

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerWillClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
